using System;
using System.Reflection;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;

namespace EntityKit.Runtime
{
    public partial class BindService
    {
        private static void BindComponent(Component component, FieldInfo field)
        {
            // 使用缓存的自定义属性提高性能
            var bindAttr = GetCustomAttributeWithCache<ComponentBindAttribute>(field);
            if (bindAttr == null) return;
            var targetComponent = FindTransformWithPathCache(component.transform, bindAttr.Path)?.GetComponent(field.FieldType);
            if (targetComponent != null)
            {
                field.SetValue(component, targetComponent);
            }
            else
            {
                Debug.LogError($"组件绑定失败：路径 {bindAttr.Path} 未找到 {field.FieldType.Name} 组件");
            }
        }
    }
}